package cn.iocoder.yudao.module.bpm.framework.flowable.core.listener.demo.exection;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmConstants;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.UserTask;

import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.task.api.Task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: 雨同我
 * @Description:
 * @DateTime: 2024/8/22 11:32
 **/

@Component
@Slf4j
public class SkipExpression implements ExecutionListener {

    @Autowired
    private TaskService taskService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void notify(DelegateExecution execution) {

        Map<String, List<Long>> map = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get("selectUser"), Map.class);

        UserTask userTask=(UserTask)execution.getCurrentFlowElement();

//        Task task = taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();
        String id = execution.getId();
        List<Task> list = taskService.createTaskQuery().executionId(id).list();

        System.out.println("task"+list+" 执行id"+id+" "+execution.getCurrentActivityId()+" ==="+execution.getProcessInstanceId());
        System.out.println("跳过"+userTask.getId()+" "+userTask.getName()+" "+userTask.getAssignee());
//        if (ObjectUtil.isNotEmpty(task)) {
//            updateTaskStatusAndReason(task.getId(), BpmTaskStatusEnum.APPROVE.getStatus(), "审批人为空，节点跳过");
//        }

//        实例id

//        Object test = redisTemplate.opsForValue().get("test");
        System.out.println("map"+map);

//        if (ObjectUtil.isNotEmpty(selectUser)) {
//            Map<String, List<Long>> userMap= (Map<String, List<Long>>) selectUser;
//            System.out.println("Map+ "+userMap);
//            userMap.forEach((k,v)->{
//                if (k.equals(execution.getCurrentActivityId())) {
//                    if (v.isEmpty()){
//                        execution.setVariable("_FLOWABLE_SKIP_EXPRESSION_ENABLED",true);
//                        userTask.setSkipExpression("${1==1}");
//                    }
//                    System.out.println("跳过"+k+" "+v);
//                }
//            });
//        }


    }



    /**
     * 更新流程任务的 status 状态、reason 理由
     *
     * @param id 任务编号
     * @param status 状态
     * @param reason 理由（审批通过、审批不通过的理由）
     */
    private void updateTaskStatusAndReason(String id, Integer status, String reason) {
        updateTaskStatus(id, status);
        taskService.setVariableLocal(id, BpmConstants.TASK_VARIABLE_REASON, reason);
    }

    private void updateTaskStatus(String id, Integer status) {
        taskService.setVariableLocal(id, BpmConstants.TASK_VARIABLE_STATUS, status);
    }

}
